﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {

        private static System.Timers.Timer timer = null;

        public Service1()
        {
            InitializeComponent();
            if (timer==null)
            {
                timer = new System.Timers.Timer();
                timer.Enabled = true;
                timer.Interval = 10000;
                timer.Elapsed+=new System.Timers.ElapsedEventHandler(timer_Elapsed);
                this.ServiceName = "MyService";

            }
        }

        protected override void OnStart(string[] args)
        {
            timer.Enabled = false;
            System.Diagnostics.Process[] myproc = System.Diagnostics.Process.GetProcessesByName("ServerForm");
            if (myproc!=null&&myproc.Length>0)
            {
                int n = myproc.Length;
                for (int i = 0; i < n; i++)
                {
                    myproc[i].Kill();

                }
            }
        }


        //定时执行
        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            System.Diagnostics.Process[] myproc = System.Diagnostics.Process.GetProcessesByName("ServerForm");
            if (myproc==null||myproc.Length==0)
            {
                //控制程序 单例执行
                System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + "ServerForm.exe");

            }
        }

        protected override void OnStop()
        {
        }

    }
}
